<?php

namespace App\Providers;

use App\Models\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use App\Exceptions\ReqException;

class AuthServiceProvider extends ServiceProvider {
	/**
	 * Register any application services.
	 *
	 * @return void
	 */
	public function register() {
		//
	}
	
	/**
	 * Boot the authentication services for the application.
	 *
	 * @return void
	 */
	public function boot() {
		$this->app ['auth']->viaRequest ( 'api', function (\Illuminate\Http\Request $request) {
			
			if ($request->has ( 'uid' ) && $request->has ( 'password' )) {
				$uid = $request->input ( 'uid' );
				$enpassword = strtolower ($request->input ( 'password' ) );
				
				$user = User::where ( 'uid', $uid )->where ( 'password', $enpassword )->first ();
				if (is_null ( $user )) {
					throw new ReqException ( '9001', '用户不存在或密码错误！' );
				}
				
				return $user;
			} else {
				throw new ReqException ( '9101', '缺少必要参数！' );
			}
		} );
	}
}
